package com.concurrency.example.aqs.test.p1;

import com.concurrency.example.aqs.test.Computable;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: xuwei
 * @Date: 2018/11/30 11:11
 */
public class Memoizer1<A, V> implements Computable<A, V> {
    private final Map<A, V> cache = new HashMap<>();
    private final Computable<A, V> c;

    public Memoizer1(Computable<A, V> c) {
        this.c = c;
    }

    public Map<A, V> getCache() {
        return cache;
    }

    @Override
    public V compute(A a) throws InterruptedException {
        V result = cache.get(a);
        if (result == null) {
            result = c.compute(a);
            cache.put(a, result);
        }
        return result;
    }
}
